package org.jeecg.modules.job.sub.service.impl;

import org.jeecg.modules.job.sub.entity.SubTitle;
import org.jeecg.modules.job.sub.entity.SubSelect;
import org.jeecg.modules.job.sub.entity.SubAnswer;
import org.jeecg.modules.job.sub.mapper.SubSelectMapper;
import org.jeecg.modules.job.sub.mapper.SubAnswerMapper;
import org.jeecg.modules.job.sub.mapper.SubTitleMapper;
import org.jeecg.modules.job.sub.service.ISubTitleService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 题目信息
 * @Author: jeecg-boot
 * @Date:   2022-10-04
 * @Version: V1.0
 */
@Service
public class SubTitleServiceImpl extends ServiceImpl<SubTitleMapper, SubTitle> implements ISubTitleService {

	@Autowired
	private SubTitleMapper subTitleMapper;
	@Autowired
	private SubSelectMapper subSelectMapper;
	@Autowired
	private SubAnswerMapper subAnswerMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(SubTitle subTitle, List<SubSelect> subSelectList,List<SubAnswer> subAnswerList) {
		subTitleMapper.insert(subTitle);
		if(subSelectList!=null && subSelectList.size()>0) {
			for(SubSelect entity:subSelectList) {
				//外键设置
				entity.setTitleId(subTitle.getId());
				subSelectMapper.insert(entity);
			}
		}
		if(subAnswerList!=null && subAnswerList.size()>0) {
			for(SubAnswer entity:subAnswerList) {
				//外键设置
				entity.setTitleId(subTitle.getId());
				subAnswerMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(SubTitle subTitle,List<SubSelect> subSelectList,List<SubAnswer> subAnswerList) {
		subTitleMapper.updateById(subTitle);
		
		//1.先删除子表数据
		subSelectMapper.deleteByMainId(subTitle.getId());
		subAnswerMapper.deleteByMainId(subTitle.getId());
		
		//2.子表数据重新插入
		if(subSelectList!=null && subSelectList.size()>0) {
			for(SubSelect entity:subSelectList) {
				//外键设置
				entity.setTitleId(subTitle.getId());
				subSelectMapper.insert(entity);
			}
		}
		if(subAnswerList!=null && subAnswerList.size()>0) {
			for(SubAnswer entity:subAnswerList) {
				//外键设置
				entity.setTitleId(subTitle.getId());
				subAnswerMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		subSelectMapper.deleteByMainId(id);
		subAnswerMapper.deleteByMainId(id);
		subTitleMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			subSelectMapper.deleteByMainId(id.toString());
			subAnswerMapper.deleteByMainId(id.toString());
			subTitleMapper.deleteById(id);
		}
	}
	
}
